class_name FloatingText
extends Node2D

const SEC_TO_MSEC: float = 1000

@onready var label: Label = $Label


var local_rng:RandomNumberGenerator = RandomNumberGenerator.new()

var text:String = ""
var duration:float = 2.0
var fadepoint:float = 0.5
var velocity:Vector2 = Vector2(0,-0.1)
var color:Color = Color.WHITE
var random_offset:float = 20.0

var time_out = Time.get_ticks_msec()


func _ready() -> void:
	label.text = text
	modulate = color
	
	position += Vector2(local_rng.randf_range(-random_offset,random_offset),local_rng.randf_range(-random_offset,random_offset))
	position.y -= 30
	
	var pos_tween = create_tween()
	var duration_msec: float = duration * SEC_TO_MSEC
	var offset : Vector2 = velocity * duration_msec
	pos_tween.tween_property(self,"position",
		position + offset,
		1.0 * duration
	).set_trans(Tween.TRANS_LINEAR)
	
	if fadepoint < duration:
		var modulate_tween = create_tween()
		var fade_duration: float = duration - fadepoint
		modulate_tween.tween_property(self,"modulate",
			Color(modulate.r,modulate.g,modulate.b,0),
			fade_duration
		).set_trans(Tween.TRANS_LINEAR).set_delay(fadepoint)
		
		
		
		
	
func _process(delta: float) -> void:
	
	if Time.get_ticks_msec() - time_out > 3000:
		queue_free()
		
	
	
	
	
	
	
	
